# Windows安装脚本
# 使用方法: .\scripts\install.ps1

Write-Host "🚀 开始安装视频会议应用..." -ForegroundColor Cyan

# 检查Node.js
Write-Host "`n📦 检查Node.js..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version
    Write-Host "✅ Node.js 版本: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ 未安装Node.js，请先安装Node.js 18+: https://nodejs.org/" -ForegroundColor Red
    exit 1
}

# 检查npm
try {
    $npmVersion = npm --version
    Write-Host "✅ npm 版本: $npmVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ npm不可用" -ForegroundColor Red
    exit 1
}

# 安装依赖
Write-Host "`n📦 安装项目依赖..." -ForegroundColor Yellow
npm install
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 依赖安装失败" -ForegroundColor Red
    exit 1
}

Write-Host "`n📦 安装子包依赖..." -ForegroundColor Yellow
npm run install:all
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 子包依赖安装失败" -ForegroundColor Red
    exit 1
}

# 创建必要的目录
Write-Host "`n📁 创建必要的目录..." -ForegroundColor Yellow
$directories = @(
    "packages/server/data",
    "packages/server/logs",
    "packages/server/uploads",
    "packages/web/logs",
    "backups"
)

foreach ($dir in $directories) {
    if (!(Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir | Out-Null
        Write-Host "✅ 创建目录: $dir" -ForegroundColor Green
    }
}

# 复制环境变量示例文件
Write-Host "`n⚙️  配置环境变量..." -ForegroundColor Yellow

if (!(Test-Path "packages/server/.env")) {
    Copy-Item "packages/server/.env.example" "packages/server/.env"
    Write-Host "✅ 已创建 packages/server/.env (请编辑配置)" -ForegroundColor Green
    Write-Host "   ⚠️  请务必修改 JWT_SECRET 和 MEDIASOUP_ANNOUNCED_IP" -ForegroundColor Yellow
} else {
    Write-Host "ℹ️  packages/server/.env 已存在，跳过" -ForegroundColor Blue
}

if (!(Test-Path "packages/web/.env.production")) {
    Copy-Item "packages/web/.env.example" "packages/web/.env.production"
    Write-Host "✅ 已创建 packages/web/.env.production (请编辑配置)" -ForegroundColor Green
} else {
    Write-Host "ℹ️  packages/web/.env.production 已存在，跳过" -ForegroundColor Blue
}

# 构建项目
Write-Host "`n🔨 构建项目..." -ForegroundColor Yellow
npm run build:all
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 构建失败" -ForegroundColor Red
    exit 1
}

# 安装PM2
Write-Host "`n📦 检查PM2..." -ForegroundColor Yellow
try {
    $pm2Version = pm2 --version
    Write-Host "✅ PM2 已安装: $pm2Version" -ForegroundColor Green
} catch {
    Write-Host "📦 安装PM2..." -ForegroundColor Yellow
    npm install -g pm2
    npm install -g pm2-windows-startup
    
    Write-Host "⚙️  配置PM2开机自启..." -ForegroundColor Yellow
    pm2-startup install
}

Write-Host "`n✅ 安装完成！" -ForegroundColor Green
Write-Host "`n下一步操作:" -ForegroundColor Cyan
Write-Host "1. 编辑 packages/server/.env 配置文件" -ForegroundColor White
Write-Host "2. 编辑 packages/web/.env.production 配置文件" -ForegroundColor White
Write-Host "3. 配置防火墙开放端口 3100-3110" -ForegroundColor White
Write-Host "4. 运行: pm2 start ecosystem.config.js" -ForegroundColor White
Write-Host "5. 运行: pm2 save" -ForegroundColor White
Write-Host "6. 访问: http://localhost:3100" -ForegroundColor White
Write-Host "`n详细部署说明请查看: deploy.md" -ForegroundColor Yellow

